package com.transac.demo.service;

import com.transac.demo.mapper.Account;
import com.transac.demo.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TransactionNestedServiceImpl implements TransactionNestedService{
    @Autowired
    AccountMapper accountMapper;
    @Autowired
    TransactionNestedServiceImpl transactionNestedService;

    @Override
    public void updateMoney1() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        transactionNestedService.updateJerryMoney1();
    }



    @Override
    @Transactional
    public void updateMoney2() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        transactionNestedService.updateJerryMoney1();
    }

    @Override
    @Transactional
    public void updateMoney3() {
        Account account = accountMapper.selectByPrimaryKey(1);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        transactionNestedService.updateJerryMoney3();
        throw new RuntimeException("error");
    }

    @Transactional(propagation = Propagation.NESTED)
    public void updateJerryMoney3() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
    }

    @Override
    @Transactional
    public void updateMoney4() {
        try {
            Account account = accountMapper.selectByPrimaryKey(1);
            account.setMoney(account.getMoney() + 100);
            accountMapper.updateByPrimaryKey(account);
            transactionNestedService.updateJerryMoney1();
        } catch (Exception e) {

        }
    }
    @Transactional(propagation = Propagation.NESTED)
    public void updateJerryMoney1() {
        Account account = accountMapper.selectByPrimaryKey(3);
        account.setMoney(account.getMoney() + 100);
        accountMapper.updateByPrimaryKey(account);
        throw new RuntimeException("error");
    }

}
